/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.clicarros.dao.Implementacao.anuncio;

import br.com.clicarros.dao.Implementacao.AbstractDAO;
import br.com.clicarros.dominio.anuncio.Anuncio;
import br.com.clicarros.dominio.veiculo.Veiculo;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import br.com.clicarros.util.UtilHibernate;

/**
 *
 * @author Bruno
 */
public class DAOAnuncio extends AbstractDAO {



    @Override
    public List<Object> consultar(Object o) throws ClassNotFoundException, SQLException, Exception {
        
       Anuncio a = (Anuncio) o;
                
       Session session = UtilHibernate.openSession();
       
       StringBuilder sql = new StringBuilder("from Anuncio as a where ");
       sql.append(" a.veiculo.modelo ilike '%");
       sql.append(a.getVeiculo().getModelo());
       sql.append("%'");
       sql.append("and a.cor ilike '%");
       sql.append(a.getCor());
       sql.append("%'");
       sql.append("and a.combustivel ilike '%");
       sql.append(a.getCombustivel());
       sql.append("%'");    
       Query q = session.createQuery(sql.toString());
       
       //q.setParameter("modelo", a.getVeiculo().getModelo());


       List<Object> retorno = q.list();
     
       return retorno;
       
    }

    @Override
    public Object consultarId(Object o) throws ClassNotFoundException, SQLException, Exception {
        
       Anuncio a = (Anuncio) o;
                
       Session session = UtilHibernate.openSession();

       a  = (Anuncio)  session.load(a.getClass(), a.getIdanuncio());  
       
       session.close();

       return a;        
    }
    
    
    
}
